<?php
require_once('../model/m_roles.php');

class maintenance_roles{

	public $data = array();
	public $add_role_name;
	public $current_role;
	
	public function actionView(){
		$this->data = getRoles(); //Get roles to populate select options		
		include_once("../views/v_session.php");
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_roles.html");
		include_once("../views/v_footer_principal.html");
	}
	
	public function actionAdd(){	
		include_once("../views/v_session.php");	
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_roles_add.html");
		include_once("../views/v_footer_principal.html");	
	}
	
	public function actionAddRole($new_role){	
		if(addRole($new_role)){
			header("location:c_maintenance_roles.php?view=list");
			exit;				
		} else {		
			echo "<h1> Error creating Role</h1>";
			exit;
		}	
	}	
	
	public function actionEditRole($roleid){		
		$current_role = array();
		$current_role = getRole($roleid);
		
		///*** FALTA MANEJAR ROLE ID INEXISTENTE
		include_once("../views/v_session.php");		
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_roles_edit.html");
		include_once("../views/v_footer_principal.html");
	}	
	
	public function actionSaveRoleEdit($roleid, $name){
		
		if(saveRoleEdit($roleid, $name)){
			header("location:c_maintenance_roles.php?view=list");
			exit;
		}
		else {
			include_once("../views/v_session.php");		
			include_once("../views/v_header_principal_lev1.html");
			echo "<h1>THERE WAS A PROBLEM WHEN TRYING TO SAVE THE CHANGES!!!</h1>";
			include_once("../views/v_footer_principal.html");
		}	
	
	}
		
	public function actionDeleteRole($roleid){
		
		if(deleteRole($roleid)){
			header("location:c_maintenance_roles.php?view=list");
			exit;
		}
		else {
			include_once("../views/v_session.php");		
			include_once("../views/v_header_principal_lev1.html");
			echo "<h2>Unable to delete Role. Make sure the role is not in use by any user.</h2>";
			echo "<br><br>";
			echo '<input type="button" value="Back" onclick="goBack();"/>';
			include_once("../views/v_footer_principal.html");
		}	
	
	}
	
	
}

$instancia= new maintenance_roles;

if(isset($_GET['view'])){
	if($_GET['view']=="list")	
		$instancia->actionView();
}

if(isset($_GET['add'])){
	if($_GET['add']=="go")	
		$instancia->actionAdd();
}

if(isset($_GET['role_name'])){
		$instancia->actionAddRole($_GET['role_name']);
}

if(isset($_GET['edit'])){
		$instancia->actionEditRole($_GET['edit']);
}

if(isset($_GET['save_role'])){
		$instancia->actionSaveRoleEdit($_GET['role_id'],$_GET['save_role']);
}

if(isset($_GET['delete'])){
		$instancia->actionDeleteRole($_GET['delete']);
}


?>